package pers.zxk.jlytnews.util;

import java.sql.*;

public class DBconn {
    //正式环境
    //static String url = "jdbc:mysql://81.70.104.45:3306/db_jlytnews?useunicuee=true&characterEncoding=utf8"; // 腾讯云数据库连接url
    //static String url = "jdbc:mysql://82.157.248.105:3306/db_jlytnews?useunicuee=true&characterEncoding=utf8"; // 腾讯云数据库连接url
    //测试环境
    static String url = "jdbc:mysql://127.0.0.1:3306/db_jlytnews?serverTimezone=UTC&useunicuee=true&characterEncoding=utf8"; // 数据库连接url
    static String username = "root"; // 数据库用户名
    static String password = "Wangqi789654!"; // 用户密码
    static Connection conn = null; // 初始化一个数据连接 conndb_jlytnews
    static ResultSet rs = null; // 初始化一个结果集 rs
    static PreparedStatement ps = null; // 初始化一个PreparedStatement对象 ps

    public static void init() throws ClassNotFoundException, SQLException {
        /*try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            System.out.println("init [SQL驱动程序初始化失败！]");
            e.printStackTrace();
        }*/

        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(url, username, password);
    }

    public static int addUpdDel(String sql) throws SQLException {
        int i = 0;
        PreparedStatement ps = conn.prepareStatement(sql);
        i = ps.executeUpdate();

        return i;
    }

    public static ResultSet selectSql(String sql) throws SQLException {
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery(sql);
        return rs;
    }

    public static void closeConn() throws SQLException {
        /*try {
            conn.close();
        } catch (SQLException e) {
            System.out.println("sql数据库关闭异常");
            e.printStackTrace();
        }*/

        conn.close();

    }

}
